package org.example.leetcpde;

import org.junit.jupiter.api.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class myAtoi {
    @Test
    void aa() {
        System.out.println(Long.MAX_VALUE);
        String aa = "0123456789";
        byte[] bytes = aa.getBytes();
        for (byte aByte : bytes) {
            System.out.println(aByte);
        }
    }


    @Test
    void myAtoi() {
        int i = myAtoi("20000000000000000000");
        i =myAtoi(" -042");
        i =myAtoi("words and 987");
        i =myAtoi("0-1");
        System.out.println(22);

    }

    public int myAtoi(String s) {
        if (s == null) {
            return 0;
        }

        s = s.trim();
        if (s.length() == 0) {
            return 0;
        }

        byte[] bytes = s.getBytes();
        byte aByte = bytes[0];

        int sign = 1;
        int begin = 0;
        if (aByte == '-') {
            sign = -1;
            begin = 1;
        } else if (aByte == '+') {
            begin = 1;
        }

        long num = 0;
        for (int i = begin; i < bytes.length; i++) {
            aByte = bytes[i];
            if (aByte >= '0' && aByte <= '9') {
                num = num * 10 +  aByte - '0';
                if (num > Integer.MAX_VALUE) {
                    return sign == 1 ?  Integer.MAX_VALUE : Integer.MIN_VALUE;
                }
            } else {
                break;
            }
        }

        return Long.valueOf(num).intValue() * sign;
    }
}
